package _7_27test;


import java.util.*;
/*
商品类 (Product)，包含以下信息：
id (唯一标识)
name (商品名称)
price (商品价格)
订单类 (Order)，包含以下信息：
id (唯一标识)
productIds (订单中包含的商品ID列表)
订单金额
一个方法或属性来存储和访问订单中每个商品的详细信息
 */

class Product {
    private String id;
    private String name;
    private double price;

    public Product(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public void setId(String id) {this.id = id;}
    public void setName(String name) {this.name = name;}
    public void setPrice(double price) {this.price = price;}
    public String getId() { return id; }
    public String getName() { return name; }
    public double getPrice() { return price; }
}

class Order {
    private String id;
    private List<String> productIds;
    private double orderPrice;

    public Order(String id, List<String> productIds, double orderPrice) {
        this.id = id;
        this.productIds = productIds;
        this.orderPrice = orderPrice;
    }

    private List<Product> products = new ArrayList<>();

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public String getId() { return id; }
    public List<String> getProductIds() { return productIds; }
    public double getOrderPrice() { return orderPrice; }
    public List<Product> getProducts() { return products; }
}

public class OrderService {
    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
                new Product("1", "手机", 1200.0),
                new Product("2", "鼠标", 100.0),
                new Product("3", "手表", 50.0)
        );

        List<Order> orders = Arrays.asList(
                new Order("1001", Arrays.asList("1", "2"), 1220.0),
                new Order("1002", Arrays.asList("1", "3"), 1250.0)
        );

        List<Order> enrichedOrders = enrichOrdersWithProducts(products, orders);

        for (Order order : enrichedOrders) {
            System.out.println("Order ID: " + order.getId());
            System.out.println("Total Amount: " + order.getOrderPrice());
            System.out.println("Products:");
            for (Product product : order.getProducts()) {
                System.out.println("ID: " + product.getId() + ", Name: " + product.getName() + ", Price: " + product.getPrice());
            }
            System.out.println();
        }
    }
    public static List<Order> enrichOrdersWithProducts(List<Product> products, List<Order> orders) {
        Map<String, Product> productMap = new HashMap<>();
        for (Product product : products) {
            productMap.put(product.getId(), product);
        }

        for (Order order : orders) {
            List<Product> orderProducts = new ArrayList<>();
            for (String productId : order.getProductIds()) {
                Product product = productMap.get(productId);
                if (product != null) {
                    orderProducts.add(product);
                }
            }
            order.setProducts(orderProducts);
        }

        return orders;
    }
}
